const { getCurrentDateTime } = require("../modal/time.config");
const STATE = {
  LOW: 0,
  UP: 1,
};

const resolveDataFun = (req, list) => {
  return new Promise((resolve, reject) => {
    let p = list.map(async (v) => {
      const arr = v.categoryId.split(",");
      v.categoryId = arr;
      const newData = await req.services.CategoryService.findById(
        arr[arr.length - 1],
      );
      v.categoryInfo = newData;
      return v;
    });
    Promise.all(p).then((res) => {
      resolve(res);
    });
  });
};

exports.create = async (req, res) => {
  const body = req.body;
  const {
    title,
    categoryId,
    content,
    introduce,
    state = STATE.UP,
    cover,
  } = body;
  const data = {
    title,
    categoryId: categoryId.join(","),
    content,
    introduce,
    state,
    cover,
    create_time: getCurrentDateTime(),
    update_time: getCurrentDateTime(),
  };
  const newData = await req.services.KnowledgeService.create(data);
  if (newData.affectedRows === 1) {
    res.status(200).json({ data: "新增成功", msg: "success", code: 200 });
  } else {
    res.status(200).json({ data: "系统错误，请重试", msg: "fail", code: 500 });
  }
};

exports.list = async (req, res) => {
  const body = req.body;
  const {
    title = "",
    categoryId = "",
    content = "",
    introduce = "",
    state = "",
    page = null,
    pageSize = null,
    desc = "id",
  } = body;
  let newData = await req.services.KnowledgeService.getList({
    where: { state },
    like: { categoryId, title, content, introduce },
    page,
    pageSize,
    desc,
  });

  let count = await req.services.KnowledgeService.getCount({
    where: { state },
    like: { categoryId, title, content, introduce },
  });
  const result = await resolveDataFun(req, newData);

  res.status(200).json({
    data: {
      list: result,
      total: count,
    },
    msg: "success",
    code: 200,
  });
};

exports.update = async (req, res) => {
  const body = req.body;
  const {
    title,
    categoryId,
    content,
    introduce,
    state = STATE.UP,
    id,
    cover,
  } = body;
  const update_time = getCurrentDateTime();
  const data = {
    title,
    categoryId: categoryId.join(","),
    content,
    introduce,
    state,
    cover,
    update_time,
  };
  const newData = await req.services.KnowledgeService.update(data, {
    id,
  });
  if (newData.affectedRows === 1) {
    res.status(200).json({ data: "更新成功", msg: "success", code: 200 });
  } else {
    res.status(200).json({ data: "系统错误，请重试", msg: "fail", code: 500 });
  }
};

exports.getDetail = async (req, res) => {
  const body = req.query;
  const { id } = body;
  const newData = await req.services.KnowledgeService.findById(id);
  const result = await resolveDataFun(req, [newData]);
  if (newData) {
    res.status(200).json({ data: result[0], msg: "success", code: 200 });
  } else {
    res.status(200).json({ data: "查无此人", msg: "fail", code: 400 });
  }
};

exports.del = async (req, res) => {
  const body = req.query;
  const { id } = body;
  const newData = await req.services.KnowledgeService.delete(Number(id));
  if (newData.affectedRows === 1) {
    res.status(200).json({ data: "删除成功", msg: "success", code: 200 });
  } else {
    res.status(200).json({ data: "查无数据", msg: "fail", code: 400 });
  }
};
